package com.xmg.dataStructure.tree;

public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {7,3,10,12,5,1,9};
        BinarySortTree tree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            tree.add(new Node(arr[i]));
        }
        tree.add(new Node(2));
        tree.infixOrder();
        tree.delNode(2);
        tree.delNode(5);
        tree.delNode(9);
        tree.delNode(12);
        tree.delNode(7);
        tree.delNode(3);
        tree.delNode(10);
        tree.delNode(1);
        System.out.println("删除后的树");
        tree.infixOrder();
    }
}

//创建二叉排序树
class BinarySortTree{
    private Node root;
    public void add(Node node){
        if(root==null){
            root = node;
        }else{
            root.add(node);
        }
    }

    public Node search(int value){
        if(root == null){
            return null;
        }else{
            return root.search(value);
        }
    }

    public Node searchParent(int value){
        if(root == null){
            return null;
        }else{
            return root.searchParent(value);
        }
    }
    //返回以node为根节点的最小节点的值
    //删除已node为根节点的最小节点
    public int delRightTreeMin(Node node){
        Node taget = node;
        while (taget.left !=null){
            taget = taget.left;
        }
        delNode(taget.value);
        return taget.value;
    }

    //删除节点
    public void delNode(int value){
        if(root==null){
            return;
        }else{
            //1、查找待删除的节点targetNode
            Node targetNode = search(value);
            if(targetNode==null){
                return;
            }
            //如果二叉树只有一个节点，并且还能找到targetNode
            if(root.left==null && root.right==null){
                root = null;
                return;
            }
            Node parent = searchParent(value);
            //要删除的节点是叶子节点
            if(targetNode.left==null && targetNode.right==null){
                if(parent.left != null && parent.left.value == value){
                    parent.left = null;
                }else if (parent.right != null && parent.right.value == value){
                    parent.right = null;
                }
            }else if(targetNode.left!=null && targetNode.right!=null){//有两个子节点
                int minVal = delRightTreeMin(targetNode.right);//待删除节点的右子树的最小值  也可以是左子树的最大值
                targetNode.value = minVal;
            }else{//只有一颗子树
                if(targetNode.left!=null){
                    if(parent==null) {
                        root = targetNode.left;
                    }else{
                        if (parent.left != null && parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {
                            parent.right = targetNode.left;
                        }
                    }
                }else{
                    if(parent==null){
                        root = targetNode.right;
                    }else {
                        if (parent.left != null && parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    }
                }
            }
        }
    }

    public void infixOrder(){
        if(root==null){
            System.out.println("二叉排序树为空，不能遍历");
        }else{
            root.infixOrder();
        }
    }
}

//创建节点
class Node{
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void add(Node node){
        if(node==null){
            return;
        }

        if(node.value<this.value){//向左遍历添加
            if(this.left==null){
                this.left = node;
            }else{
                this.left.add(node);
            }
        }else{//向右遍历添加
            if(this.right==null){
                this.right = node;
            }else{
                this.right.add(node);
            }
        }
    }

    public void infixOrder(){
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    /**
     * 查找要删除的节点
     * @param value
     * @return
     */
    public Node search(int value){
        if(this.value == value){
            return this;
        }else if(value < this.value){
            if(this.left==null){
                return null;
            }
            return this.left.search(value);
        }else{
            if(this.right==null){
                return null;
            }
            return this.right.search(value);
        }
    }
    //查找要删除节点的父节点 如果没有返回null
    public Node searchParent(int value){
        if((this.left!=null && this.left.value == value) || (this.right != null && this.right.value == value)){
            return this;
        }else{
            if(value < this.value && this.left !=null){
                return this.left.searchParent(value);
            }else if(value >= this.value && this.right != null){
                return this.right.searchParent(value);
            }else{
                return null;
            }
        }
    }
}
